class Tools::WebFetionsController < ApplicationController
  
  before_filter :authorize, :except => [:index_in_show, :show, :send_sm, :friends]
  layout 'base.html', :only => :show
  
  def index
    @setting = UserToolSetting.find(params[:setting_id])
  end
  
  def show
    @tool = Tool.find_by_code('web_fetion')
  end
  
  def destroy
    @setting = UserToolSetting::WebFetion.find(params[:id])
    render :text => '', :status => @setting.destroy ? 200 : 500
  end
  
  def index_in_show
    @tool = Tool.find_by_code('web_fetion')
    @settings = UserToolSetting::WebFetion.find_all_by_user_id_and_tool_id(current_user_id, @tool.id)
  end
  
  def new_in_show
    @tool = Tool.find_by_code('web_fetion')
    @setting = UserToolSetting::WebFetion.new(:column => @tool.default_column)
  end
  
  def create_in_show    
    @tool = Tool.find_by_code('web_fetion')
    @setting = UserToolSetting::WebFetion.new(params[:user_tool_setting_web_fetion])
    @setting.attributes = {
      :user_id => current_user_id,
      :tool_id => @tool.id
    }
    if @setting.save
      redirect_to :action => :index_in_show, :format => :js
    else
      render :new_in_show
    end
  end
  
  def edit_in_show    
    @setting = UserToolSetting::WebFetion.find(params[:id])
    @tool = Tool.find_by_code('web_fetion')
  end
  
  def update_in_show
    @setting = UserToolSetting::WebFetion.find(params[:id])
    @tool = @setting.tool
    if @setting.update_attributes(params[:user_tool_setting_web_fetion])
      redirect_to :action => :index_in_show, :format => :js
    else
      render :edit_in_show
    end
  end
  
  def destroy_in_show
    @setting = UserToolSetting::WebFetion.find(params[:id])
    render :text => '', :status => @setting.destroy ? 200 : 500
  end
  
  def send_sm
    send_sm_and_render do      
      if params[:fetion_sm][:password].blank? 
        logger.debug '采用保存的飞信手机号发送'
        params[:fetion_sm][:mobile] = current_user.mobile if current_user.try(:mobile)
        params[:fetion_sm][:password] = current_user.fetion_password if current_user.try(:fetion_password)
      end
      params[:fetion_sm][:verifyCode] = params[:verify_code]
      params[:fetion_sm][:verifyImgId] = params[:verify_image_id]
      UserToolSetting::WebFetion.new(:user => current_user).syn_send_sm!(params[:fetion_sm])
      # remember_fetion
      session[:mobile] = params[:fetion_sm][:mobile]
      session[:fetion_password] = params[:fetion_sm][:password]
    end
  end
  
  def friends   
    if params[:password].blank? 
      logger.debug '采用保存的飞信手机号发送'
      params[:mobile] = current_user.mobile if current_user.try(:mobile)
      params[:password] = current_user.fetion_password if current_user.try(:fetion_password)
    end
    @friends = FetionFriend.find(:all, :params => params)
    render(:status => 500, :text => @friends.first.state) if @friends.any? and @friends.first.state != 'SUCCESS'
    # remember_fetion
    session[:mobile] = params[:mobile]
    session[:fetion_password] = params[:password]
  end
end
